package com.example.questionnaire.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.dao.QuestionnaireMapper;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.pojo.DO.Questionnaire;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.LoginPageService;
import com.example.questionnaire.service.QuestionnaireSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import static com.example.questionnaire.util.CommonUtil.*;

/**
 * 问卷设置
 */
@RestController
@RequestMapping("/QuestionnaireSettingController")
@Api(tags = "问卷设置")
public class QuestionnaireSettingController {



    @Autowired
    private QuestionnaireSettingService questionnaireSettingService;
    @Autowired
    AppidAndSecretService appidAndSecretService;


    /**
     * 问卷设置
     *
     * @param formid（问卷id）
     * @param dateTimeRange（开始及结束日期）
     * @param ceiling（问卷上限数量）
     * @param repeatAnswer（是否开启自定义重复作答提示）
     * @param repeatTip（自定义重复作答内容）
     * @param handling（是否设置提交问卷后处理方式）
     * @param shareWithTheQuery（是否设置分享与查询）
     * @param ipLimit（ip地址限制 作答次数与作答类型）

     */
    @PostMapping("/QuestionnaireSetting")
    @ApiOperation(value = "问卷设置", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse QuestionnaireSetting(@RequestParam String formid,
                                               @RequestParam String dateTimeRange,
                                               @RequestParam int ceiling,
                                               @RequestParam Boolean repeatAnswer,
                                               @RequestParam String repeatTip,
                                               @RequestParam Boolean handling,
                                               @RequestParam Boolean shareWithTheQuery,
                                               @RequestParam Boolean isIp,
                                               @RequestParam String ipLimit,
                                               @RequestParam Boolean isWechat,
                                               @RequestParam String weChatLimit,
                                               @RequestParam Boolean isPhone,
                                               @RequestParam String phoneLimit,
                                               @RequestParam String beforeTheStartOfTheClues,
                                               @RequestParam String afterTheEndOfTheClues,
                                               @RequestParam String questionnaireLimitTip) throws ParseException, IOException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        boolean b = questionnaireSettingService.setting(formid, dateTimeRange, ceiling, repeatAnswer, repeatTip, handling, shareWithTheQuery, isIp, ipLimit, isWechat,weChatLimit, isPhone, phoneLimit,beforeTheStartOfTheClues, afterTheEndOfTheClues, questionnaireLimitTip);
        if (b) {
            return ServerResponse.createBySuccess("设置成功");
        }
        return ServerResponse.createByError("设置失败");
    }




    /**
     * @param formid（问卷id）
     * @param type（1为自定义图文内容2为跳转外链）
     * @param content（自定义图文内容或者跳转外链）
     * @param handling（是否设置）

     */
    @PostMapping("/handlingWayAfterSummit")
    @ApiOperation(value = "提交问卷后处理方式", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse handlingWayAfterSummit(@RequestParam String formid, @RequestParam int type, @RequestParam String content, @RequestParam Boolean handling) throws ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        if (questionnaireSettingService.handlingWayAfterSummit(formid, type, content, handling)) {
            return ServerResponse.createBySuccess("保存成功");
        } else {
            return ServerResponse.createByError("保存失败");
        }
    }


    @GetMapping("/getHandlingWayAfterSummit")
    @ApiOperation(value = "获取提交问卷后处理方式", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse getHandlingWayAfterSummit(@RequestParam String formid) throws ParseException {

        return ServerResponse.createBySuccess(questionnaireSettingService.getHandlingWayAfterSummit(formid));
    }


    /**
     * @param formid（问卷id）
     * @param isTheTitle（正标题）
     * @param subtitle（副标题）
     * @param isAllowedToShare（是否允许分享）
     * @param imgUrl（分享图链接）
     * @param shareWithTheQuery（是否设置）

     */
    @PostMapping("/SetupSharing")
    @ApiOperation(value = "设置分享", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse SetupSharing(@RequestParam String formid,
                                       @RequestParam String isTheTitle,
                                       @RequestParam String subtitle,
                                       @RequestParam Boolean isAllowedToShare,
                                       @RequestParam String imgUrl,
                                       @RequestParam Boolean shareWithTheQuery) throws ParseException{
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

        boolean b = questionnaireSettingService.SetupSharing(formid, isTheTitle, subtitle, isAllowedToShare, imgUrl, shareWithTheQuery);
        if (b) {
            return ServerResponse.createBySuccess("保存成功");
        }
        return ServerResponse.createByError("保存失败");
    }




    /**
     * 问卷设置
     *
     * @param formid（问卷id）
     *

     */
    @GetMapping("/GetQuestionnaireSetting")
    @ApiOperation(value = "获取问卷设置", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse GetQuestionnaireSetting(@RequestParam String formid) {

        return ServerResponse.createBySuccess(questionnaireSettingService.getSetting(formid));

    }


    /**
     *
     * @param formid（问卷id）
     * @param
     * @return
     * @throws ParseException
     */
    @GetMapping("/GetSettingsSharing")
    @ApiOperation(value = "获取设置分享", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse GetSettingsSharing(@RequestParam String formid) {


        return ServerResponse.createBySuccess(questionnaireSettingService.GetSettingsSharing(formid));

    }
}
